package com.ycx.ycxmybatismanagement.controller;

import com.ycx.ycxmybatismanagement.pojo.Result;
import com.ycx.ycxmybatismanagement.util.QiniuUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;  
import org.springframework.http.ResponseEntity;  
import org.springframework.web.bind.annotation.PostMapping;  
import org.springframework.web.bind.annotation.RequestParam;  
import org.springframework.web.bind.annotation.RestController;  
import org.springframework.web.multipart.MultipartFile;  
  
import java.io.IOException;  
  
@RestController  
public class QiniuUploadController {  
  
    @Autowired  
    private QiniuUploadUtils qiniuUploadUtils;

        @PostMapping("/upload")
        public Result<String> uploadFile(@RequestParam("file") MultipartFile file) {
            try {
                String fileUrl = qiniuUploadUtils.uploadFileToQiniu(file);
                if (fileUrl != null) {
                    return Result.success(fileUrl);
                } else {
                    return  Result.error("Upload failed");
                }
            } catch (Exception e) {
                e.printStackTrace();
                return  Result.error("Upload failed: " + e.getMessage());
            }
        }
}